﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using DZRemoteController.Rules;

namespace DZRemoteController
{
	
	class RuleManager
	{
		List<IRule> rules = new List<IRule>();

		public RuleManager()
		{
			LoadRules();
		}

		private void LoadRules()
		{
			RulesConfigSection rulesSection = (RulesConfigSection)ConfigurationManager.GetSection("rulesSection");
			if (rulesSection == null)
				return;
			rules.Clear();
			foreach (RuleElement configRule in rulesSection.Rules)
			{
				IRule rule = null;
				switch (configRule.RuleType)
				{
					case "SayIt":
						rule = new SayItRule();
						break;
					case "CodesLeft":
						rule = new CodesLeftRule();
						break;
					case "MissionNo":
						rule = new MissionNoRule();
						break;
					case "EventNo":
						rule = new EventNoRule();
						break;
					case "DisConnect":
						rule = new ConnectDisconnectRule();
						break;
					default:
						throw new ApplicationException("Неизвестное правило");
				}
				rule.Init(configRule);
				rules.Add(rule);
			}
		}

		public void ApplyRules(string document)
		{
			GameController.State.LastEventNo = 0;
			foreach (IRule rule in rules)
			{
				if (rule.Validate(document))
				{
					rule.Perform();
				}
			}
		}
	}
}
